//
//  RwdRetweetView.swift
//  Weibo_rwd
//
//  Created by 任文迪 on 16/7/13.
//  Copyright © 2016年 spaceX. All rights reserved.
//

import UIKit
import SnapKit

class RwdRetweetView: UIView {
    
    private lazy var weiboImage: RwdPictureView = {
        
        let weibo = RwdPictureView()
        //  设置配图的背景色跟转发微博的背景色一致
        weibo.backgroundColor = self.backgroundColor
        
        return weibo
    }()
    
    //设置一个约束
    var retweetViewBottom : Constraint?

    //懒加载contentLabel
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.blackColor(), fontSize: 14)
        
        label.numberOfLines = 0
        
        return label
    }()
    //设置模型
    var statusModel: RwdStatusViewModel? {
        
        didSet{
            contentLabel.text = statusModel?.retweetContens
            
            retweetViewBottom?.uninstall()
            
            let count = statusModel?.status?.retweeted_status?.pic_urls?.count
            
            print(count)
            
            if count > 0 {
                
                weiboImage.pictureUrls = statusModel?.status?.retweeted_status?.pic_urls
                
                print(weiboImage.pictureUrls?.count)
            
                weiboImage.hidden = false
            
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(weiboImage.snp_bottom).offset(margin).constraint
                })
            
            }else {
                weiboImage.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.retweetViewBottom = make.bottom.equalTo(contentLabel.snp_bottom).offset(margin).constraint
                })
                
            }
            
        }
        
    }
    
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置约束
    private func setUpUI() {
        
        backgroundColor = UIColor(white: 0.8, alpha: 1)
  
        addSubview(contentLabel)
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(margin)
            make.leading.equalTo(self).offset(margin)
            make.trailing.equalTo(self).offset(-margin)
            
            addSubview(weiboImage)
            
            weiboImage.snp_makeConstraints(closure: { (make) -> Void in
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
                make.leading.equalTo(contentLabel)
                
            })
            
            self.snp_makeConstraints(closure: { (make) -> Void in
               self.retweetViewBottom = make.bottom.equalTo(weiboImage).offset(margin).constraint//记录约束
            })
            
        }
        
    }
    

}
